﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using JobDescOnline.Presentation.Models;
using eq.CoreBase.View;
using eq.CoreBase.Control;

namespace JobDescOnline.Presentation.Controllers
{
    public class LevelCompetencyController : Controller
    {
        public JobDescDBEntities1 db = new JobDescDBEntities1();

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    LevelCompetency levelcompetency = new LevelCompetency
                    {
                        Rowstatus = true,
                        CompetencyID = Convert.ToInt32(collection["CompetencyID"]),
                        Level = Convert.ToInt16(collection["Level"]),
                        Definition = collection["Definition"],
                        CreatedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT",
                        CreatedOn = DateTime.Now
                    };

                    db.LevelCompetencies.AddObject(levelcompetency);
                    db.SaveChanges();
                }

                int id = Convert.ToInt32(collection["CompetencyID"]);
                var model = db.LevelCompetencies.Where(d => d.CompetencyID == id);
                return View(model);
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    LevelCompetency levelcompetency = db.LevelCompetencies.First(d => d.ID == id);
                    levelcompetency.Rowstatus = true;
                    levelcompetency.CompetencyID = Convert.ToInt32(collection["CompetencyID"]);
                    levelcompetency.Level = Convert.ToInt16(collection["Level"]);
                    levelcompetency.Definition = collection["Definition"];
                    levelcompetency.LastModifiedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT";
                    levelcompetency.LastModifiedOn = DateTime.Now;

                    db.LevelCompetencies.ApplyCurrentValues(levelcompetency);
                    db.SaveChanges();
                }

                int ID = Convert.ToInt32(collection["CompetencyID"]);
                var model = db.LevelCompetencies.Where(d => d.CompetencyID == ID);
                return View(model);
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            LevelCompetency levelcompetency = db.LevelCompetencies.Single(d => d.ID == id);
            db.LevelCompetencies.DeleteObject(levelcompetency);

            var CompetencyID = levelcompetency.CompetencyID;
            db.SaveChanges();

            var model = db.LevelCompetencies.Where(d => d.CompetencyID == CompetencyID);
            return View(model);
        }

        [HttpPost]
        public ActionResult Level(FormCollection collection)
        {
            try
            {
                int CompetencyID = Convert.ToInt32(collection["CompetencyID"]);
                ViewData["ComboLevelCompetency"] = LevelCompetencyModel.LevelCompetencyID(CompetencyID);

                return View();
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
